angular.module('service.auth', ['ngResource', 'ngCookies'])

//login
    .service('LoginService', ['$resource', '$cookieStore', function ($resource, $cookieStore) {
        // 增加user
        var userid = null;
        var username = null;
        //login
        this.login = function (user, success, error) {
            var resource = $resource(authurl+'login', {}, {
                login: {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: function (data, headersGetter) {
                        var str = [];
                        for (var d in data)
                            str.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
                        return str.join("&");
                    }
                }
            });
            //
			var loginuser = {
				"username": user.username,
				"password": hex_hmac_md5("fjswxxjsyxgstyrz", user.password)
			};
            //
            var login_ret = resource.login({}, loginuser, success, error);
            login_ret.$promise.then(function(userinfo){
                userid = userinfo.userid;
                username = userinfo.username;
            });
            return login_ret;
        };

        //注销登陆
        this.userLogout = function () {
            $cookieStore.remove("_user");
        };
        //记录地址跳转
        this.SavePageList = function(page){
            var pageList;
            if($cookieStore.get("page_list")){
                pageList = $cookieStore.get("page_list");
            }else{
                pageList = [];
            }
            for(var i = 0;i<pageList.length;i++){
                if(page.name == pageList[i].name){
                    pageList.splice(i,1);
                }
            }
            if(pageList.length>=6){
                pageList.splice(0,1);
            }
            pageList.push(page);
            $cookieStore.put("page_list",pageList);
            return pageList;
        };
        //logout
        // this.logout = function () {
        //     var resource = $resource(authurl+'logout');
        //     //
        //     resource.save({});
        // }
        //relogin
        // this.ensure_login = function() {
        //     if (userid) {
        //         return get_userinfo();
        //     } else {
        //         var resource = $resource(authurl+'user/info');
        //         var userinfo_ret = resource.get();
        //         return userinfo_ret;
        //     }
        // }
        //get user info
        // this.get_userinfo = function() {
        //     return {userid:userid, username:username};
        // }
    }])
    .service('RegisterService',['$resource',function($resource) {
        //通用注册接口
        this.register = function (user, successFn, errorFn) {
            var data=$resource(authurl+"user/register");
            var registeruser = {
				"username": user.username,
				"password": hex_hmac_md5("fjswxxjsyxgstyrz", user.password)
			};
			return data.save(registeruser, successFn, errorFn);
        }
        // /*价媒专用注册登录接口*/
        // this.regist = function (userInfoNew, successFn, errorFn) {
        //     var data=$resource(resource+"user");
        //     return data.save(userInfoNew, successFn, errorFn);
        // }
        /*价媒专用注册信息*/
        this.addInfo = function (userInfoNew, successFn, errorFn) {
            var data=$resource(resource+"info");
            return data.save(userInfoNew, successFn, errorFn);
        }
        //添加默认权限
        this.addPower = function (userInfo, successFn, errorFn) {
            var data=$resource(resource+"power");
            return data.save(userInfo, successFn, errorFn);
        }
         //通过userid查询权限
         this.getPower = function (userid) {
             var data=$resource(resource+"power/:id");
             return data.get({id:userid});
         }

    }]);

;